﻿using BaseFramework.ConnectionProvider;
using BaseFramework.DbConnectionProvider;
using System.Collections.Generic;
using System.Linq;

namespace UnitTest
{
    public class Helper
    {
        public static Dictionary<string, string> settings_MsSQL
        {
            get
            {
                var dict = new Dictionary<string, string>();

                var sqlKey = "Ms SQL";
                var connStr = GetConnectionStr("MsSQL"); //"Persist Security Info=False;server=.\\DevR2;database=TestDB;User ID=sa;password=123456;Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1";
                var providerName = GetProviderName("MsSQL");
                var dialect = "Dialect.MsSqlDialect, Haode.Dialect.MsSqlDialect";
                dict.Clear();
                dict.Add("Key", sqlKey);
                dict.Add("ConnectionString", connStr);
                dict.Add("ProviderName", providerName);
                dict.Add("DialectClass", dialect);

                return dict;
            }
        }
        public static Dictionary<string, string> settings_MySQL
        {
            get
            {
                var dict = new Dictionary<string, string>();

                var sqlKey = "My SQL";
                var connStr = GetConnectionStr("MySQL"); //"Persist Security Info=False;server=.\\DevR2;database=TestDB;User ID=sa;password=123456;Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1";
                var providerName = GetProviderName("MySQL");
                var dialect = "Dialect.MySqlDialect, Haode.Dialect.MySqlDialect";
                dict.Clear();
                dict.Add("Key", sqlKey);
                dict.Add("ConnectionString", connStr);
                dict.Add("ProviderName", providerName);
                dict.Add("DialectClass", dialect);

                return dict;
            }
        }
        public static Dictionary<string, string> settings_MsSQL_GSMH
        {
            get
            {
                var dict = new Dictionary<string, string>();

                var sqlKey = "GSMH";
                var connStr = GetConnectionStr("GSMH"); //"Persist Security Info=False;server=.\\DevR2;database=TestDB;User ID=sa;password=123456;Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1";
                var providerName = GetProviderName("GSMH");
                var dialect = "Dialect.MsSqlDialect, Haode.Dialect.MsSqlDialect";
                dict.Clear();
                dict.Add("Key", sqlKey);
                dict.Add("ConnectionString", connStr);
                dict.Add("ProviderName", providerName);
                dict.Add("DialectClass", dialect);

                return dict;
            }
        }

        public static int totalInsert
        {
            get { return GetAppSettings("totalInsert", 200000); }
        }


        public static IDbConnectionProvider Provider
        {
            get { return ConnectionProviderFactory.Instance.Create("MsSQL_GSMH", Helper.settings_MsSQL_GSMH); }
        }


        public static string GetConnectionStr(string key)
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[key].ConnectionString;
        }
        public static string GetProviderName(string connStringName)
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[connStringName].ProviderName;
        }
        public static string GetAppSettings(string key)
        {
            if (System.Configuration.ConfigurationManager.AppSettings.AllKeys.Contains(key))
                return System.Configuration.ConfigurationManager.AppSettings[key];
            else
                return null;
        }
        public static int GetAppSettings(string key, int _default = -1)
        {
            string str = GetAppSettings(key);
            int result = 0;
            if (int.TryParse(str, out result))
                return result;
            else
                return _default;
        }
    }
}